package com.yuandian.management.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yuandian.api.management.entity.ConfGraph;
import com.yuandian.api.management.entity.ConfNodeProperty;
import com.yuandian.api.management.entity.ConfSegment;
import com.yuandian.api.management.entity.ConfSegmentResourceRelation;
import com.yuandian.api.management.validate.DeleteFlag;
import com.yuandian.api.management.validate.InsertFlag;
import com.yuandian.api.management.validate.SelectFlag;
import com.yuandian.api.management.validate.UpdateFlag;
import com.yuandian.bpm.common.core.util.R;
import com.yuandian.management.service.ITRoadMapService;
import com.yuandian.management.vo.ITRoadVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.groups.Default;
import java.util.List;
import java.util.Map;

/**
 * 说明：通用接口配置Controller
 * @Author zfsn
 * @Date 2023/2/21 9:38
 */
@RestController
@RequestMapping("/itRoadMap")
@Api(tags = "业务系统配置-IT路径图配置")
public class ITRoadMapController {

	@Autowired
	private ITRoadMapService iTRoadMapService;

	/**
	 * 通过系统id 获取it路径图
	 * @param systemId 系统id
	 * @return 通用返回对象
	 */
	@ApiOperation(value = "获取it路径图", notes = "获取it路径图")
	@GetMapping("/{systemId}")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "systemId", value = "系统id", paramType = "path", required = true, dataType = "Integer")
	})
	public R<List<Map<String, Object>>> queryByCode(@PathVariable("systemId") Integer systemId) {
		return R.ok(iTRoadMapService.getRoadMap(systemId));
	}

	/**
	 * 增加节点
	 * @return 通用返回对象
	 */
	@ApiOperation(value = "增加节点", notes = "增加节点")
	@PostMapping("/addNode")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "systemId", value = "系统id", paramType = "body", required = true, dataType = "Integer"),
			@ApiImplicitParam(name = "locationX", value = "坐标x", paramType = "body", required = true, dataType = "Double"),
			@ApiImplicitParam(name = "locationY", value = "坐标y", paramType = "body", required = true, dataType = "Double"),
			@ApiImplicitParam(name = "protocolCode", value = "协议编码", paramType = "body", required = true, dataType = "String"),
			@ApiImplicitParam(name = "name", value = "网端名称", paramType = "body", required = true, dataType = "String"),
			@ApiImplicitParam(name = "type", value = "网端类型", paramType = "body", required = true, dataType = "Integer"),
			@ApiImplicitParam(name = "sign", value = "端标识，0：非首尾端(默认)、1：首端、2：尾端", paramType = "body", required = true, dataType = "Integer"),
			@ApiImplicitParam(name = "isContinue", value = "是否与上一端联系，0：否(默认)、1：是", paramType = "body", required = true, dataType = "Integer"),
			@ApiImplicitParam(name = "isBroadcast", value = "是否广播，0：否(默认网络数据交互类型)、1：是(只做转发网络数据内容不变)", paramType = "body", required = true, dataType = "Integer"),
			@ApiImplicitParam(name = "isNat", value = "是否NAT，0：否(默认)、1：是", paramType = "body", required = true, dataType = "Integer"),
			@ApiImplicitParam(name = "isKey", value = "是否为关键网端，0：否(默认)、1：是", paramType = "body", required = true, dataType = "Integer"),
			@ApiImplicitParam(name = "isFoundByFront", value = "是否通过前一端发现当前端IP，0：否(默认)、1：是", paramType = "body", required = true, dataType = "Integer"),
			@ApiImplicitParam(name = "isDrift", value = "是否存在漂移，0：否(默认)、1：是", paramType = "body", required = true, dataType = "Integer")
	})
	public R<String> addNode(@RequestBody @Validated(InsertFlag.class) @ApiIgnore ITRoadVo itRoadVo) {
		return iTRoadMapService.addNode(itRoadVo);
	}

	/**
	 * 修改网端节点
	 * @return 通用返回对象
	 */
	@ApiOperation(value = "修改网端节点", notes = "修改网端节点")
	@PostMapping("/updateNodeById")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "id", value = "网端id", paramType = "body", required = true, dataType = "Integer"),
			@ApiImplicitParam(name = "systemId", value = "系统id", paramType = "body", required = true, dataType = "Integer"),
			@ApiImplicitParam(name = "protocolCode", value = "协议编码", paramType = "body", required = true, dataType = "String"),
			@ApiImplicitParam(name = "name", value = "网端名称", paramType = "body", required = true, dataType = "String"),
			@ApiImplicitParam(name = "type", value = "网端类型", paramType = "body", required = true, dataType = "Integer"),
			@ApiImplicitParam(name = "sign", value = "端标识，0：非首尾端(默认)、1：首端、2：尾端", paramType = "body", required = true, dataType = "Integer"),
			@ApiImplicitParam(name = "isContinue", value = "是否与上一端联系，0：否(默认)、1：是", paramType = "body", required = true, dataType = "Integer"),
			@ApiImplicitParam(name = "isBroadcast", value = "是否广播，0：否(默认网络数据交互类型)、1：是(只做转发网络数据内容不变)", paramType = "body", required = true, dataType = "Integer"),
			@ApiImplicitParam(name = "isNat", value = "是否NAT，0：否(默认)、1：是", paramType = "body", required = true, dataType = "Integer"),
			@ApiImplicitParam(name = "isKey", value = "是否为关键网端，0：否(默认)、1：是", paramType = "body", required = true, dataType = "Integer"),
			@ApiImplicitParam(name = "isFoundByFront", value = "是否通过前一端发现当前端IP，0：否(默认)、1：是", paramType = "body", required = true, dataType = "Integer"),
			@ApiImplicitParam(name = "isDrift", value = "是否存在漂移，0：否(默认)、1：是", paramType = "body", required = true, dataType = "Integer")
	})
	public R<String> updateNodeById(@RequestBody @ApiIgnore ITRoadVo itRoadVo) {
		return iTRoadMapService.updateNodeById(itRoadVo);
	}

	/**
	 * 增加节点连线
	 * @return 通用返回对象
	 */
	@ApiOperation(value = "增加节点连线", notes = "增加节点连线")
	@PostMapping("/addLink")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "systemId", value = "系统id", paramType = "body", required = true, dataType = "Integer"),
			@ApiImplicitParam(name = "nodeId", value = "节点id, 与网端id对应", paramType = "body", required = true, dataType = "String"),
			@ApiImplicitParam(name = "parentNodeId", value = "父节点id, 与网端id对应", paramType = "body", required = true, dataType = "String"),
	})
	public R<String> addLink(@Validated({InsertFlag.class, Default.class}) @RequestBody @ApiIgnore ConfGraph confGraph) {
		return iTRoadMapService.addLink(confGraph);
	}


	/**
	 * 删除节点连线
	 * @return 通用返回对象
	 */
	@ApiOperation(value = "删除节点连线", notes = "删除节点连线")
	@PostMapping("/removeLink")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "systemId", value = "系统id", paramType = "body", required = true, dataType = "Integer"),
			@ApiImplicitParam(name = "nodeId", value = "节点id, 与网端id对应", paramType = "body", required = true, dataType = "String"),
			@ApiImplicitParam(name = "parentNodeId", value = "父节点id, 与网端id对应", paramType = "body", required = true, dataType = "String"),
	})
	public R<String> removeLink(@Validated({InsertFlag.class, Default.class}) @RequestBody @ApiIgnore ConfGraph confGraph) {
		return iTRoadMapService.removeLink(confGraph);
	}

	/**
	 * 删除节点
	 * @return 通用返回对象
	 */
	@ApiOperation(value = "删除节点", notes = "删除节点")
	@PostMapping("/removeNode")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "systemId", value = "系统id", paramType = "body", required = true, dataType = "Integer"),
			@ApiImplicitParam(name = "id", value = "网端id", paramType = "body", required = true, dataType = "Integer")
	})
	public R<String> removeNode(@Validated({DeleteFlag.class, Default.class}) @RequestBody @ApiIgnore ConfSegment confSegment) {
		return iTRoadMapService.removeNode(confSegment);
	}

	/**
	 * 移动节点
	 * @return 通用返回对象
	 */
	@ApiOperation(value = "移动节点", notes = "移动节点")
	@PostMapping("/setLocation")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "systemId", value = "系统id", paramType = "body", required = true, dataType = "Integer"),
			@ApiImplicitParam(name = "nodeId", value = "节点id", paramType = "body", required = true, dataType = "String"),
			@ApiImplicitParam(name = "locationX", value = "坐标x", paramType = "body", required = true, dataType = "Double"),
			@ApiImplicitParam(name = "locationY", value = "坐标y", paramType = "body", required = true, dataType = "Double"),
	})
	public R<String> setLocation(@Validated({UpdateFlag.class}) @RequestBody @ApiIgnore ConfNodeProperty confNodeProperty) {
		return iTRoadMapService.setLocation(confNodeProperty);
	}


	/**
	 * 供网端监控详情 远程调用的 it路径图接口
	 * @return 通用返回对象
	 */
	@ApiIgnore
	@PostMapping("/segmentMonitorDetailItRoadGraph")
	public R<List<Map<String, Object>>> segmentMonitorDetailItRoadGraph(@Validated(SelectFlag.class) @RequestBody ConfNodeProperty confNodeProperty) {
		return R.ok(iTRoadMapService.segmentMonitorDetailItRoadGraph(confNodeProperty));
	}

	/**
	 * 获取服务资源
	 * @return 通用返回对象
	 */
	@ApiOperation(value = "获取服务资源", notes = "获取服务资源")
	@PostMapping("/queryComponentServerResourceList")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "segmentId", value = "网端id或者节点id", paramType = "body", required = true, dataType = "Integer"),
			@ApiImplicitParam(name = "systemId", value = "系统id", paramType = "body", required = true, dataType = "Integer"),
			@ApiImplicitParam(name = "pageSize", value = "每页大小", paramType = "body", required = true, dataType = "Integer"),
			@ApiImplicitParam(name = "pageNum", value = "当前页数", paramType = "body", required = true, dataType = "Integer"),
	})
	public R<Page<Map<String,Object>>> getComponentServerResources(@RequestBody @ApiIgnore ConfSegmentResourceRelation confSegmentResourceRelation) {
		return R.ok(iTRoadMapService.getComponentServerResources(confSegmentResourceRelation));
	}

	/**
	 * 添加组件资源
	 * @return 通用返回对象
	 */
	@ApiOperation(value = "添加组件资源", notes = "添加组件资源")
	@PostMapping("/addComponentResource")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "segmentId", value = "网端id或者节点id", paramType = "body", required = true, dataType = "Integer"),
			@ApiImplicitParam(name = "systemId", value = "系统id", paramType = "body", required = true, dataType = "Integer"),
			@ApiImplicitParam(name = "resourceStr", value = "服务资源", paramType = "body", required = true, dataType = "String"),
	})
	public R<String> addComponent(@RequestBody @ApiIgnore ConfSegmentResourceRelation confSegmentResourceRelation) {
		return iTRoadMapService.addComponent(confSegmentResourceRelation);
	}


	/**
	 * 删除组件资源
	 * @return 通用返回对象
	 */
	@ApiOperation(value = "删除组件资源", notes = "删除组件资源")
	@PostMapping("/deleteComponentResource/{ids}")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "ids", value = "组件资源id(可以多选)", paramType = "path", required = true, dataType = "Integer"),
	})
	public R<String> deleteComponentResourceByIds(@PathVariable("ids") List<Integer> ids) {
		return iTRoadMapService.deleteComponentResourceByIds(ids);
	}


}
